package com.zego.wawaji.wawaji_client;

import android.app.Activity;
import android.app.Application;
import android.content.Context;

import com.app.hubert.library.ScreenUtils;
import com.tencent.bugly.crashreport.CrashReport;
import com.zego.wawaji.base.module.ModuleMgr;
import com.zego.wawaji.base.observer.PActivityLifecycleCallbacks;
import com.zego.wawaji.ui.view.adDialog.utils.DisplayUtil;
import com.zego.wawaji.wawaji_client.util.PreferenceUtil;

/**
 * des: 自定义Application.
 */
public class App extends Application {
    public static Context sApplicationContext;
    public static Activity activity;
    private static PActivityLifecycleCallbacks lifecycleCallbacks;
    public static int statusBarHeight = 0;  // 设备状态栏高度，全局唯一获取一次
    public static long t;  // 记录应用启动时间
    public static String cookie = "";
    public static boolean isLogin = false;
    public static long uid = 0;//全局uid，避免重复从本地获取

    public static boolean isNewUser = false; // 是否是新用户

    @Override
    public void onCreate() {
        super.onCreate();

        sApplicationContext = this;

        // bugly初始化
        CrashReport.initCrashReport(getContext(), "ff5f8410d7", false);
        CrashReport.setUserId(PreferenceUtil.getInstance().getUserID());

        t = System.currentTimeMillis();
        lifecycleCallbacks = new PActivityLifecycleCallbacks();
        registerActivityLifecycleCallbacks(lifecycleCallbacks);
        ModuleMgr.initModule(this);
        statusBarHeight = ScreenUtils.getStatusBarHeight(this);
        DisplayUtil.initDisplayOpinion(this);

        // LeakCanary
//        if (!LeakCanary.isInAnalyzerProcess(this)) {
//            LeakCanary.install(this);
//        }
    }

    public static Context getContext() {
        return sApplicationContext;
    }

    /**
     * @return 获取当前展示的activity对象，如果activity为null则返回applicationContext
     */
    public static Activity getActivity() {
        return activity == null ? (Activity) sApplicationContext : activity;
    }

    /**
     * @return 判断最后的Activity是否属于前台显示
     */
    public static boolean isForeground() {
        return lifecycleCallbacks.isForeground();
    }
}
